package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer... checkitemIds) {
        //先保存检查组信息
        checkGroupDao.add(checkGroup);
        //保存检查组和检查项的中间表
        if(checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
            }
        }

    }

    @Override
    public PageResult findpage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page=checkGroupDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public CheckGroup findById(int id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> selectCheckitemIdsById(int id) {
        return checkGroupDao.selectCheckitemIdsById(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer... checkitemIds) {
        //修改检查组表
        checkGroupDao.edit(checkGroup);
        //删除检查组和检查项的中间表的数据
        checkGroupDao.deleteByCheckgroupId(checkGroup.getId());
        //重新增加中间表数据
        if(checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
            }
        }
    }

    @Override
    public void delete(int id) {
        //根据检查组ID删除中间表
        checkGroupDao.deleteByCheckgroupId(id);
        //删除检查组
        checkGroupDao.delete(id);
    }

    @Override
    public List<CheckGroup> findAll() {

        return checkGroupDao.findAll();
    }
}
